package lib.self.ex.fragment.list;

import android.os.Handler;

import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.List;

import lib.self.ConstEx.ListConstEx;
import lib.self.ConstEx.ViewState;
import lib.self.R;
import lib.self.ex.activity.list.SRRecyclerActivityEx.LoadingType;
import lib.self.ex.response.IListResponse;
import lib.self.network.bean.NetworkResponse;
import lib.self.network.error.NetError;

/**
 * description: 刷新和加载更多
 * author: Steven Xiao
 * date: 2017/5/23
 */
public abstract class SRRecyclerFragmentEx<T> extends RecyclerFragmentEx<T> {
    public int mOffset;
    public LoadingType mLoadingType = LoadingType.refresh;
    private SmartRefreshLayout mRefreshLayout;

    @Override
    public void findViews() {
        super.findViews();
        mRefreshLayout = findViewById(R.id.refresh_layout);
    }

    @Override
    public int getContentViewId() {
        return R.layout.sr_recyclerview_match_parent;
    }

    @Override
    public void setViewsValue() {
        super.setViewsValue();

        mRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                SRRecyclerFragmentEx.this.onLoadMore();
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                SRRecyclerFragmentEx.this.onRefresh();
            }
        });

        refresh();
    }

    public abstract void getDataFromNet();

    protected void refresh() {
        getRefreshLayout().autoRefresh();

        setViewState(ViewState.KViewStateNormal);
    }

    public void onRefresh() {
        hideNoMoreDataView();
        mLoadingType = LoadingType.refresh;
        mOffset = 0;
        getDataFromNet();
    }

    public void onLoadMore() {
        mLoadingType = LoadingType.load_more;
        getDataFromNet();
    }

    public SmartRefreshLayout getRefreshLayout() {
        return mRefreshLayout;
    }

    protected void setEnalbeRefresh(boolean enable) {
        getRefreshLayout().setEnableRefresh(enable);
    }

    protected void setEnableLoadMore(boolean enable) {
        getRefreshLayout().setEnableLoadMore(enable);
    }

    public abstract IListResponse<T> parseNetworkResponse(int what, String text);

    @Override
    public Object onNetworkResponse(int id, NetworkResponse nr) {
        return parseNetworkResponse(id, nr.getText());
    }

    @Override
    public void onNetworkError(int id, NetError error) {
        super.onNetworkError(id, error);
        if (getLoadingType() == LoadingType.refresh) {
            finishRefreshing(false);
            if (getData() == null) {
                new Handler().postDelayed(() -> setViewState(ViewState.KViewStateFailed), 1000);
            } else {
                showToast("请检查网络连接");
            }
        } else if (getLoadingType() == LoadingType.load_more) {
            finishLoadMore(false);
        }
    }

    @Override
    public void onNetworkSuccess(int id, Object result) {
        IListResponse response = (IListResponse) result;
        List data = response.getData();
        if (getLoadingType() == LoadingType.refresh) {
            setData(data);
            invalidate();
            finishRefreshing(true);
            if (data == null || data.isEmpty()) {
                showEmptyView();
                hideNoMoreDataView();
            } else {
                hideEmptyView();
            }
            if (data != null && data.size() >= getLimit()) {
                setEnableLoadMore(true);
                updateOffset();
            } else {
                setEnableLoadMore(false);
                if (data != null && !data.isEmpty() && data.size() < getLimit()) {
                    showNoMoreDataView();
                }
            }

        } else {
            finishLoadMore(true);
            if (!response.getData().isEmpty()) {
                addAll(data);
                invalidate();
                if (response.getData().size() >= getLimit()) {
                    setEnableLoadMore(true);
                    updateOffset();
                } else {
                    setEnableLoadMore(false);
                    showNoMoreDataView();
                }
            } else {
                setEnableLoadMore(false);
                showNoMoreDataView();
            }
        }
        setViewState(ViewState.KViewStateNormal);
    }

    protected void finishRefreshing(boolean success) {
        getRefreshLayout().finishRefresh(success);
    }

    protected void finishLoadMore(boolean success) {
        getRefreshLayout().finishLoadMore(success);
    }

    public int getLimit() {
        return ListConstEx.KDefaultLimit;
    }

    public int getOffset() {
        return mOffset;
    }

    public void updateOffset() {
        mOffset = mOffset + getLimit();
    }

    public LoadingType getLoadingType() {
        return mLoadingType;
    }

}